package com.weiwei.diancan.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weiwei.diancan.R;
import com.weiwei.diancan.bean.LoginBean;
import com.weiwei.diancan.bean.MenuBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import weiwei.com.weiweilibrary.views.ToastUtil;


/**
 * 右侧主界面ListView的适配器
 * 
 * @author Administrator
 * 
 */
public class HomeOpenAdapter extends BaseAdapter {
	private Context context;
	private List<LoginBean.MenuListBean> foodDatas;
	private List<MenuBean> tabBeen;
	private OnItemClick onItemClick;
	private Map<Integer,String> isSelect = new HashMap<>();

	public void setOnItemClick(OnItemClick onItemClick) {
		this.onItemClick = onItemClick;
	}

	public void clearSelect(){
		isSelect.clear();
		notifyDataSetChanged();
	}

	public HomeOpenAdapter(Context context, List<LoginBean.MenuListBean> foodDatas) {
		this.context = context;
		this.foodDatas = foodDatas;
		setData(foodDatas);
	}

	public void setData(List<LoginBean.MenuListBean> foodDatas){
		tabBeen = new ArrayList<>();
		for(int i = 0;i<foodDatas.size();i++){
			List<LoginBean.MenuListBean.DishListBean> tab_list = foodDatas.get(i).getDish_list();
			for(int j = 0;j<tab_list.size();j++){
				tabBeen.add(new MenuBean(tab_list.get(j),foodDatas.get(i).getType_id(),foodDatas.get(i).getType_name()));
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(tabBeen!=null){
			return tabBeen.size();
		}else {		
			return 10;
		}		
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHold holder = null;
		if(arg1 == null){
			arg1 = View.inflate(context, R.layout.item_open_detail, null);
			holder = new ViewHold();
			holder.tv_title = (TextView)arg1.findViewById(R.id.tv_fenlei);
			holder.tv_name = (TextView)arg1.findViewById(R.id.tv_name);
			holder.tv_price = (TextView)arg1.findViewById(R.id.tv_price);
			holder.tv_jj = (TextView)arg1.findViewById(R.id.tv_jj);
			holder.tv_num = (TextView)arg1.findViewById(R.id.tv_num);
			holder.tv_add = (TextView)arg1.findViewById(R.id.tv_add);
			holder.ll_order = (LinearLayout)arg1.findViewById(R.id.ll_order);
			holder.iv_tao = (ImageView)arg1.findViewById(R.id.iv_tao);
			arg1.setTag(holder);
		}else {
			holder = (ViewHold)arg1.getTag();
		}

		holder.ll_order.setOnClickListener(v -> {
            if(onItemClick!=null){
                onItemClick.onAllClick(arg0);
            }
        });

		holder.tv_title.setText(tabBeen.get(arg0).getTitle());
		if(arg0 == 0){
			holder.tv_title.setVisibility(View.VISIBLE);
		}else if (!TextUtils.equals(tabBeen.get(arg0).getTitle(),tabBeen.get(arg0 -1).getTitle())) {
			holder.tv_title.setVisibility(View.VISIBLE);
		}else {
			holder.tv_title.setVisibility(View.GONE);
		}
		LoginBean.MenuListBean.DishListBean dishListBean = tabBeen.get(arg0).getTabListBean();

		if(dishListBean.getLog_type().equals("1")){   //套餐
			holder.iv_tao.setVisibility(View.VISIBLE);
			holder.tv_name.setText(dishListBean.getSet_name());
			holder.tv_price.setText(dishListBean.getSet_price()+"/"+dishListBean.getSet_unit());
		}else if(dishListBean.getLog_type().equals("0")){ //单品
			holder.iv_tao.setVisibility(View.GONE);
			holder.tv_name.setText(dishListBean.getDish_name());
			holder.tv_price.setText(dishListBean.getDish_price()+"/"+dishListBean.getDish_unit());
		}

		final ViewHold finalHolder = holder;
		holder.tv_add.setOnClickListener(v -> {
			int num = Integer.parseInt(finalHolder.tv_num.getText().toString());
			finalHolder.tv_num.setText((num+1)+"");
			if(onItemClick!=null){
				onItemClick.onAdd(tabBeen.get(arg0),arg0,finalHolder.tv_num);
				isSelect.put(arg0,finalHolder.tv_num.getText().toString());
			}
		});

		holder.tv_jj.setOnClickListener(v -> {
			int num = Integer.parseInt(finalHolder.tv_num.getText().toString());
			if(num == 0){
				ToastUtil.showToast(context,"不能再减啦！");
			}else{
				finalHolder.tv_num.setText((num-1)+"");
				if(onItemClick!=null){
					onItemClick.onAjj(tabBeen.get(arg0),arg0,finalHolder.tv_num);
					isSelect.put(arg0,finalHolder.tv_num.getText().toString());
				}
			}

		});

		if(isSelect.get(arg0)!=null){
			holder.tv_num.setText(isSelect.get(arg0));
		}else {
			holder.tv_num.setText("0");
		}
		return arg1;
	}
	private static class ViewHold{
		private TextView tv_title;
		private TextView tv_name;
		private TextView tv_price;
		private TextView tv_jj;
		private TextView tv_num;
		private TextView tv_add;
		private ImageView iv_tao;
		private LinearLayout ll_order;
	}

	public interface OnItemClick{
		void onAdd(MenuBean menuBean,int pos,TextView textView);

		void onAjj(MenuBean menuBean,int pos,TextView textView);

		void onAllClick(int pos);
	}
}
